package com.hongning.railway.ui.main.fragments.threesessionsandonelesson;

import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.app.AppViewModelFactory;
import com.hongning.railway.databinding.ActivityThreesessionsandonelessonInfoBinding;
import com.hongning.railway.entity.reqbean.ThreesessionSanDoneLessonBean;
import com.hongning.railway.ui.categorylistnews.NewDetailFragmentViewModel;
import com.hongning.railway.util.GlideUtils;
import com.hongning.railway.util.RetrofitClient;

import es.voghdev.pdfviewpager.library.RemotePDFViewPager;
import es.voghdev.pdfviewpager.library.adapter.PDFPagerAdapter;
import es.voghdev.pdfviewpager.library.remote.DownloadFile;
import es.voghdev.pdfviewpager.library.util.FileUtil;
import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class ThreesessionSanDoneLessonFragmentInfo extends BaseFragment<ActivityThreesessionsandonelessonInfoBinding,ThreesessionSanDoneLessonInfoViewModel> implements     DownloadFile.Listener{
    RemotePDFViewPager remotePDFViewPager;
    ThreesessionSanDoneLessonBean.DataBean dataBean;
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.activity_threesessionsandonelesson_info;
    }
    public static ThreesessionSanDoneLessonFragmentInfo newInstance() {
        Bundle args = new Bundle();
        ThreesessionSanDoneLessonFragmentInfo fragment = new ThreesessionSanDoneLessonFragmentInfo();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public int initVariableId() {
        return BR.tslInfoViewModel;
    }

    @Override
    public void initData() {
        super.initData();
        dataBean = (ThreesessionSanDoneLessonBean.DataBean)getArguments().getSerializable("ThreesessionSanDoneLessonBean");
        binding.congtentTxt.setText(dataBean.getMeetingissue()==null?"暂无":dataBean.getMeetingissue());
        viewModel.setData(dataBean);
        if (dataBean.getAttachinfos()!=null&&!dataBean.getAttachinfos().isEmpty()&&dataBean.getAttachinfos().get(0)!=null)
        {
            if (dataBean.getAttachinfos().get(0).getFilepath().contains("pdf")){
//                ToastUtils.showShort("pdf");
                remotePDFViewPager = new RemotePDFViewPager(getActivity(), RetrofitClient.baseUrl_1+dataBean.getAttachinfos().get(0).getFilepath(), this);
                remotePDFViewPager.setId(R.id.pdfViewPager);
                binding.infoImg.setVisibility(View.GONE);
                binding.webview.setVisibility(View.GONE);
            }else if (dataBean.getAttachinfos().get(0).getFilepath().contains("jpg")||dataBean.getAttachinfos().get(0).getFilepath().contains("jpeg")||dataBean.getAttachinfos().get(0).getFilepath().contains("png")){
                //加载图片
                GlideUtils.loadImageView(getActivity(),RetrofitClient.baseUrl_1+dataBean.getAttachinfos().get(0).getFilepath(),binding.infoImg);
//                ToastUtils.showShort("加载图片");
                binding.pdfViewPager.setVisibility(View.GONE);
                binding.pbBar.setVisibility(View.GONE);
                binding.webview.setVisibility(View.GONE);
            }else if (dataBean.getAttachinfos().get(0).getFilepath().contains("doc")||dataBean.getAttachinfos().get(0).getFilepath().contains("docx")||dataBean.getAttachinfos().get(0).getFilepath().contains("xls")
                    ||dataBean.getAttachinfos().get(0).getFilepath().contains("xlsx")||dataBean.getAttachinfos().get(0).getFilepath().contains("ppt")||dataBean.getAttachinfos().get(0).getFilepath().contains("pptx")){
                binding.infoImg.setVisibility(View.GONE);
                binding.pdfViewPager.setVisibility(View.GONE);
                binding.pbBar.setVisibility(View.GONE);
                binding.webview.loadUrl(RetrofitClient.baseUrl_1+dataBean.getAttachinfos().get(0).getFilepath());
            }else {
                ToastUtils.showShort("位置格式文件");
                binding.pbBar.setVisibility(View.GONE);
            }


        }else {
            binding.pbBar.setVisibility(View.GONE);
        }

    }

    @Override
    public ThreesessionSanDoneLessonInfoViewModel initViewModel() {
        AppViewModelFactory appViewModelFactory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this,appViewModelFactory).get(ThreesessionSanDoneLessonInfoViewModel.class);

    }



    @Override
    public void onSuccess(String url, String destinationPath) {

        binding.pbBar.setVisibility(View.GONE);
        PDFPagerAdapter adapter = new PDFPagerAdapter(getActivity(), FileUtil.extractFileNameFromURL(url));
        remotePDFViewPager.setAdapter(adapter);
        updateLayout();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
if (remotePDFViewPager!=null)
{
    ((PDFPagerAdapter) remotePDFViewPager.getAdapter()).close();
}
if (binding.webview!=null)
{
    binding.webview.destroy();
}
    }
    /*更新视图*/
    private void updateLayout() {
        binding.remotePdfRoot.removeAllViewsInLayout();
        binding.remotePdfRoot.addView(remotePDFViewPager, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
    }

    @Override
    public void onFailure(Exception e) {
        binding.pbBar.setVisibility(View.GONE);
//        .show(this, "数据加载错误");
        ToastUtils.showShort("数据加载错误");
    }

    @Override
    public void onProgressUpdate(int progress, int total) {

    }
}
